<?php

include _ITSTOCK_INCLUDE_DIR_.'html_search.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_message.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_navigation.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_links.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_menu.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_table_list.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'hardware_html.inc.php';

$itstock_page_info['css'] = array('main.css', 'aside.css', 'float_menu.css', 'page.css', 'table.css', 'lightbox.css', 'form_save.css');
$itstock_page_info['js'] = array('common.js', 'ajax.js', 'request.js', 'lightbox.js', 'search.js', 'settings.js');
$itstock_page_info['title'] = 'Hardware - IT Stock Tool';
$itstock_page_info['menu_title'] = 'Hardware';
$itstock_page_info['menu_items'] = array('<label onclick="itstock_page_request(\'add_hardware\')">Add Hardware</label>');
$itstock_page_info['search_page'] = 'hardware';

$_SESSION['on_delete']['hardware'] = $_SERVER['REQUEST_URI'];
$_SESSION['on_save']['hardware'] = $_SERVER['REQUEST_URI'];

$structure = itstock_html_search_init('hardware');
$structure['table_headers'] = array('Type', 'Maker', 'Model', 'Capacity', 'Serial', 'Condition', '', '', '', '');
$structure['table_indexes'] = array('category', 'maker', 'model', 'capacity', 'serialn', 'state');
$structure['table_actions'] = array(
  'location' => null,
  'order' =>  null,
  array( 'img' => 'img/edit.png', 'onclick' => 'itstock_page_request(\'add_hardware\', \'%s\')', 'title' => 'Edit', 'id' => 'id' ),
  array( 'link' => 'delete.php?type=hardware&id=%s', 'img' => 'img/delete.png', 'title' => 'Delete', 'id' => 'id')
);
$structure['table_error'] = 'No hardware have been added.';

$cat_get = $_GET;
if(isset($cat_get['idx'])) unset($cat_get['idx']);
if(isset($cat_get['search'])) unset($cat_get['search']);
if(isset($_GET['cat'])) {
  $structure['where'][0] = 'category_id='.itstock_decode($_GET['cat']);
  array_shift($structure['table_headers']);
  array_shift($structure['table_indexes']);
}

itstock_html_base_header($itstock_page_info);
echo '<div class="aside">';
itstock_structure($cate, 'category');
$menu_title = array(
  'text' => 'All Hardware',
  'url' => '?page=hardware'
);
itstock_html_menu_struct($cate, 'category', 'id', 'cat', '', $menu_title, $cat_get);
echo '</div>';
echo '<div class="bside">';
itstock_html_menu_search($itstock_page_info);
echo '<table>';
if(itstock_search_all($structure)){
  itstock_html_table_list_navigation($structure);
  ?>
  <tr>
  <td colspan="2">
  <table class="list">
  <?php
  itstock_html_table_list_headers($structure['table_headers']);
  echo '<tbody>';
  foreach($structure['data'] as &$item){
    echo '<tr>';
    foreach($structure['table_indexes'] as $idx) {
      echo '<td>'.$item[$idx].'</td>';
    }
    if(is_null($item['order_item_id'])) {
      $structure['table_actions']['order'] = array( 'img' => 'img/order_disabled.png', 'title' => 'Order Not Found');
    } else {
      $structure['table_actions']['order'] = array( 'link' => 'main.php?page=order_item&id=%s', 'img' => 'img/order.png', 'title' => 'See Order', 'id' => 'order_item_id');
    }
    if(!is_null($item['cpu_id'])) {
      $structure['table_actions']['location'] = array( 'link' => 'main.php?page=hardware_cpu&id=%s', 'img' => 'img/case.png', 'title' => 'Show Location', 'id' => 'cpu_id');
    } else {
      if(!is_null($item['location_id'])) {
        $structure['table_actions']['location'] = array( 'link' => 'main.php?page=hardware_location&id=%s', 'img' => 'img/location.png', 'title' => 'Show Location', 'id' => 'location_id');
      }
      else {
        $structure['table_actions']['location'] = array( 'link' => 'main.php?page=hardware_ware&id=%s', 'img' => 'img/warehouse.png', 'title' => 'Show Warehouse', 'id' => 'warehouse_id');
      }
    }
    itstock_html_table_list_links($structure['table_actions'], $item, 'list_center');
  echo '</tr>';
  }
} else {
  echo '<table><tr><td>'.$structure['table_error'].'</td></tr>';
}
echo '</tbody></table></td></tr>';
echo '</table>';
echo '</div>';
itstock_html_base_footer();
?>
